package com.book.scc.controller;

import com.book.scc.pojo.Book;
import com.book.scc.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/queryBooks.do")
    public String queryBook(HttpServletRequest request,
                            @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1")int pageNum,
                            @RequestParam(value = "bookname",required = false,defaultValue = "")String bookname){
        // Pagehelper  分页
        // startPage  起始页
        PageHelper.startPage(pageNum,2);
        List books = bookService.queryBooks(bookname);
        PageInfo pageInfo = new PageInfo(books);
        request.getSession().setAttribute("books",books);
        request.getSession().setAttribute("bookname",bookname);
        request.getSession().setAttribute("pageInfo",pageInfo);
        return "forward:books.jsp";
    }

    @RequestMapping("/queryBookById.do")
    public String queryBookById(int id,HttpServletRequest request){
        Book book = bookService.queryBookById(id);
        request.setAttribute("book",book);
        return "forward:update.jsp";
    }

    @RequestMapping("/deleteBook.do")
    public String deleteBook(int id){
        System.out.println("进来");
        int i = bookService.deleteBook(id);
        System.out.println("出来");
        return "forward:/queryBooks.do";
    }

    @RequestMapping("/updateBook.do")
    public String updateBook(Book book){
        int i = bookService.updateBook(book);
        return "forward:/queryBooks.do";
    }

    @RequestMapping("/queryAdd.do")
    public String queryAdd(Book book){
        System.out.println("aaa");
        int i = bookService.queryAdd(book);
        return "forward:/queryBooks.do";
    }
}
